package com.xyp.mesadmin.md.type.service.impl;

import com.xyp.mesadmin.md.type.VO.SelectTypeVO;
import com.xyp.mesadmin.md.type.mapper.MdItemTypeMapper;
import com.xyp.mesadmin.md.type.service.MdItemTypeService;
import com.xyp.mesadmin.md.type.VO.MdItemTypeVO;
import com.xyp.mescommon.entity.md.MdItemType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/9/26
 * @Solgne 从想做到能做
 * @Desc
 */
@Service
public class MdItemTypeServiceImpl implements MdItemTypeService {

    @Autowired
    private MdItemTypeMapper mdItemTypeMapper;
    //查询
    @Override
    public List<MdItemTypeVO> findAllItemType() {
        return mdItemTypeMapper.selectAllItemType();
    }
    //根据id查询
    @Override
    public MdItemType findById(Integer id) {
        return mdItemTypeMapper.selectById(id);
    }
    //删除
    @Override
    public void delete(Integer id) {
        List<MdItemType> mdItemTypes = mdItemTypeMapper.selectByParentTypeId(id);
        if (mdItemTypes != null && mdItemTypes.size() > 0) {
            throw new RuntimeException("该类型存在子类，无法删除");
        }
        mdItemTypeMapper.deleteById(id);
    }

    @Override
    public void add(MdItemType mdItemType) {
        MdItemType mdItemType1 = mdItemTypeMapper.selectById(mdItemType.getParentTypeId().intValue());
        String ancestors = mdItemType1.getAncestors() + "," + mdItemType1.getId();
        mdItemType.setAncestors(ancestors);
        mdItemTypeMapper.insertItemType(mdItemType);
    }

    @Override
    public void update(MdItemType mdItemType) {
        mdItemTypeMapper.updateItemType(mdItemType);
    }

    @Override
    public List<SelectTypeVO> selectTypeVO() {
        List<SelectTypeVO> selectTypeVOS = mdItemTypeMapper.selectTypeVO();
        return selectTypeVOS;
    }

}
